package gov.dhs.cis.soa.readers;

import gov.dhs.cis.soa.SOAData;
import gov.dhs.cis.soa.Utils;
import gov.dhs.cis.soa.data.Rule;
import gov.dhs.cis.soa.data.RuleSet;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class RulesLayerReader extends ExcelReader {
	static final Logger loger = Logger.getLogger(RulesLayerReader.class);

	private final String RULES_FILE_NAMES = "RCT_RuleSets.xls;Process_RuleSets.xls";
	//private final String RULES_FILE_NAMES = "Process_RuleSets.xls";
	private String RSET_SHEET_NAME = "RuleSets";

	private String RSET_COL_NUM = "B";
	private String RSET_DESC_COL_NUM = "C";
	private String RSET_DOMAIN_COL_NUM = "E";

	private String RSHEET_RULE_COL_NUM = "B";
	private String RSHEET_DESC_COL_NUM = "D";

	public SOAData extractRuleSets(SOAData soaData) {
		loger.debug("Extracting RuleSets (Definations) layer Sheet...");
		String[] sheetNames = RULES_FILE_NAMES.split("[;]");

		ArrayList<RuleSet> rsets = new ArrayList<RuleSet>();
		soaData.rSetsList = rsets;
		
		for (String sName : sheetNames) {
			HSSFWorkbook w = openFile(sName);
			HSSFSheet s1 = w.getSheet(RSET_SHEET_NAME);
			int rows = s1.getLastRowNum();
			loger.debug("S1 rows =" + rows);

			for (int n = 1; n <= rows; n++) {
				HSSFRow row = s1.getRow(n);
				if (row == null)
					continue;
				String data = getCellStringValue(w, row, RSET_COL_NUM);
				if (Utils.isEmpty(data)) {
					loger.warn("RuleSet Name is Null/Empty at row : " + n + ", ignoring this row.");
					continue;
				}
				RuleSet rObj = new RuleSet();
				rObj.id = Utils.getNextId();
				rObj.rSetId = (n+1);
				rObj.name = data;
				rObj.descText = getCellStringValue(w, row, RSET_DESC_COL_NUM);
				rObj.domain = getCellStringValue(w, row, RSET_DOMAIN_COL_NUM);
				rObj.createDescription();
				
				rObj = extractRules(rObj, w);
				rsets.add(rObj);
			}
			closeAll(w);
		}
		return soaData;
	}

	public RuleSet extractRules(RuleSet rSet, HSSFWorkbook w) {
		String sheetName = rSet.rSetId + "-" + rSet.name;
		loger.debug("Extracting Rule for " + sheetName + " layer Sheet...");

		HSSFSheet s1 = w.getSheetAt(rSet.rSetId-1);
		int rows = s1.getLastRowNum();
		loger.debug("S1 rows =" + rows);

		boolean bFoundRowHeader = false;
		for (int n = 0; n <= rows; n++) {
			HSSFRow row = s1.getRow(n);
			if (row == null)
				continue;

			String ruleName = getCellStringValue(w, row, RSHEET_RULE_COL_NUM);
			if (Utils.isEmpty(ruleName)) {
				loger.warn("Rule Name is Null/Empty at row : " + n + ", ignoring this row.");
				continue;
			}
			if (!bFoundRowHeader) {
				if ("Rule Name".equalsIgnoreCase(ruleName)) {
					bFoundRowHeader = true;
				}
				continue;
			}

			Rule sOp = new Rule();
			sOp.name = ruleName;
			sOp.id = Utils.getNextId();
			sOp.descTxt = getCellStringValue(w, row, RSHEET_DESC_COL_NUM);
			sOp.createDescription();
			rSet.add(sOp);
		}
		return rSet;
	}
}
